package com.katsu.myapplib.secret;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

final class r {

    private static final int KEYSIZE = 1024;

    public static final String ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding";

    public static final String RSA = "RSA";

    public static byte[] efd(byte[] d, PublicKey ic) {

        try {

            Cipher c = Cipher.getInstance(ECB_PKCS1_PADDING);

            c.init(Cipher.ENCRYPT_MODE, ic);

            int l = d.length;

            ByteArrayOutputStream o = new ByteArrayOutputStream();

            int os = 0;

            int mbs = KEYSIZE / 8 - 11;

            for (int i = 0; l - os > 0; os = i * mbs) {

                byte[] cc;

                if (l - os > mbs) {

                    cc = c.doFinal(d, os, mbs);

                } else {

                    cc = c.doFinal(d, os, l - os);

                }

                o.write(cc, 0, cc.length);

                ++i;

            }

            byte[] ed = o.toByteArray();

            o.close();

            return ed;

        } catch (Exception e) {

            e.printStackTrace();

            return null;

        }

    }

    public static byte[] dfd(byte[] d, PrivateKey te) {

        try {

            Cipher c = Cipher.getInstance(ECB_PKCS1_PADDING);

            c.init(Cipher.DECRYPT_MODE, te);

            int l = d.length;

            ByteArrayOutputStream o = new ByteArrayOutputStream();

            int os = 0;

            int mbs = KEYSIZE / 8;

            for (int i = 0; l - os > 0; os = i * mbs) {

                byte[] cc;

                if (l - os > mbs) {

                    cc = c.doFinal(d, os, mbs);

                } else {

                    cc = c.doFinal(d, os, l - os);

                }

                o.write(cc, 0, cc.length);

                ++i;

            }

            byte[] ed = o.toByteArray();

            o.close();

            return ed;

        } catch (Exception e) {

            return null;

        }

    }

    public static PublicKey ic(String cks) throws Exception {

        try {

            byte[] b = k.de(cks);

            KeyFactory kf = KeyFactory.getInstance(RSA);

            X509EncodedKeySpec ks = new X509EncodedKeySpec(b);

            return kf.generatePublic(ks);

        } catch (NoSuchAlgorithmException e) {

            throw new Exception("无此算法");

        } catch (InvalidKeySpecException e) {

            throw new Exception("公钥非法");

        } catch (NullPointerException e) {

            throw new Exception("公钥数据为空");

        }

    }

    public static PrivateKey te(String pks) throws Exception {

        try {

            byte[] b = k.de(pks);

            PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(b);

            KeyFactory kf = KeyFactory.getInstance(RSA);

            return kf.generatePrivate(ks);

        } catch (NoSuchAlgorithmException e) {

            throw new Exception("无此算法");

        } catch (InvalidKeySpecException e) {

            throw new Exception("私钥非法");

        } catch (NullPointerException e) {

            throw new Exception("私钥数据为空");

        }

    }

    public static PublicKey ic(InputStream in) throws Exception {

        try {

            return ic(rk(in));

        } catch (IOException e) {

            throw new Exception("公钥数据流读取错误");

        } catch (NullPointerException e) {

            throw new Exception("公钥输入流为空");

        }

    }

    public static PrivateKey te(InputStream in) throws Exception {

        try {

            return te(rk(in));

        } catch (IOException e) {

            throw new Exception("私钥数据读取错误");

        } catch (NullPointerException e) {

            throw new Exception("私钥输入流为空");

        }

    }

    private static String rk(InputStream in) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        String rl;

        StringBuilder sb = new StringBuilder();

        while ((rl = br.readLine()) != null) {

            if (rl.charAt(0) == '-') {

                continue;

            } else {

                sb.append(rl);

                sb.append('\r');

            }

        }

        return sb.toString();

    }
}
